﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.CRM.Bussiness;
using ZX.CRM.Library;
using ZX.Web.UI.Controls.ControlBind;
namespace ZX.WebApp.Crm.Modules
{
    public partial class AddInvoiceInfo : EditUIPage
    {
        private string strId = "", strOrderId = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            BTCrmCustomer btc = new BTCrmCustomer();
            strOrderId = this.GetStringFromQueryString("strOrderId", "");
            strId = this.GetStringFromQueryString("id", "");
            for (int i = 2012; i <= DateTime.Now.Year; i++)
            {
                ListItem li = new ListItem();
                li.Text = i.ToString();
                li.Value = i.ToString();
                ddlYear.Items.Add(li);
            }
            //产品包
            BCrmProductPack bllPack = new BCrmProductPack();
            IList<CrmProductPack> ListProductPack = bllPack.GetAllProductPack();
            DropDownListControl.DropDataSourceBind(this.ddlDetailsPE, ListProductPack, "Packname", "ID");

            if (GetStringFromQueryString("view", "") == "check")
            {
                try
                {
                    V_CrmProductPayValue cp = btc.GetV_CrmPayInfo(new Guid(strId));
                    if (cp != null)
                    {
                        if (cp.CheckAddUserName == "")
                        {
                            string rValue = bllPack.ReturnDefaultValue(new Guid(strId));
                            if (rValue.Contains('|'))
                            {
                                string[] strArr = rValue.Split('|');
                                if (strArr.Length == 8)
                                {
                                    ddlIsFifteenMonth.SelectedValue = strArr[0];
                                    ddlIsProjectOver.SelectedValue = strArr[1];
                                    ddlIsCEOInnerTrain.SelectedValue = strArr[2];
                                    ddlIsInnerTrainSian.SelectedValue = strArr[3];
                                    ddlIsFisrtSale.SelectedValue = strArr[4];
                                    ddlEP.SelectedValue = strArr[5];
                                    ddlSelfhelp.SelectedValue = strArr[6];
                                    ddlIsYj.SelectedValue = strArr[7];
                                }
                            }
                        }
                        else
                        {
                            ddlIsFifteenMonth.SelectedValue = getSelectValue(cp.IsFifteenMonth);
                            ddlIsFisrtSale.SelectedValue = getSelectValue(cp.IsFisrtSale);
                            int Year = cp.FactPayDate.Year - cp.HmDate.Year;
                            int Month = Year * 12 + (cp.FactPayDate.Month - cp.HmDate.Month);
                            if (cp.IsFifteenMonth == "")
                            {
                                if (Month > 15)//15个月内
                                    ddlIsFifteenMonth.SelectedValue = "0";
                                else
                                    ddlIsFifteenMonth.SelectedValue = "1";
                            }
                            if (cp.IsFisrtSale == "")
                            {
                                if (Month > 4) //一推4个月内或现场回款
                                    ddlIsFisrtSale.SelectedValue = "0";
                                else
                                    ddlIsFisrtSale.SelectedValue = "1";
                            }
                            if (cp.IsYj == "否")
                            {
                                ddlIsCEOInnerTrain.SelectedValue = getSelectValue(cp.IsCEOInnerTrain);
                            }
                            ddlIsYj.SelectedValue = getSelectValue(cp.IsYj);
                            ddlIsInnerTrainSian.SelectedValue = getSelectValue(cp.IsInnerTrainSian);
                            ddlIsProjectOver.SelectedValue = getSelectValue(cp.IsProjectOver);
                            ddlSelfhelp.SelectedValue = cp.IsSelfHelp;
                            ddlEP.SelectedValue = cp.IsPEContinu;
                        }
                        ddlDetailsPE.SelectedValue = cp.ProductPackID.ToString();
                        ddlYear.SelectedValue = cp.SelfHelpYear;
                        lblCourse.Text = cp.CourseLayoutName;
                        lblDev.Text = cp.OriginName;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else
            {
                CrmPay cp = btc.GetCrmPayInfo(new Guid(strId));
                txtMoney.Value = cp.InvoiceMoney == 0 ? "" : cp.InvoiceMoney.ToString();
                txtInvoince.Value = cp.InvoiceNum;
                txtReceipt.Value = cp.Receipt;
                ExpressNo.Value = cp.ExpressNo;
            }
        }
        private string getSelectValue(string str)
        {
            if (str == "是")
            {
                str = "1";
            }
            else if (str == "否")
            {
                str = "0";
            }
            else if (str == "不确定")
            {
                str = "2";
            }
            else str = "";
            return str;
        }
        public string StrId
        { get { return strId; } }
        public string StrOrderId
        { get { return strOrderId; } }
    }
}